package com.avasthi.cli.twitter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

import de.l3s.boilerpipe.BoilerpipeProcessingException;
import de.l3s.boilerpipe.extractors.ArticleExtractor;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;
import net.minidev.json.parser.JSONParser;
import net.minidev.json.parser.ParseException;

public class TwitterJsonParserUserDirectory {

	static Set<String> userIdTweetId = new HashSet<String>();

	static void writeText(JSONObject object, PrintWriter writer) {
		String txt = (String) (object.get("text"));
		writer.println(txt);
	}

	static void writeHashTags(JSONArray hashtags, PrintWriter writer) {

		for (JSONObject h : hashtags.toArray(new JSONObject[hashtags.size()])) {
			String hashtagCategory = h.get("text").toString().toLowerCase();
			writer.print(hashtagCategory + " ");
		}
		writer.println();
	}

	static void writeURLs(JSONArray urls, PrintWriter writer) {

		for (JSONObject u : urls.toArray(new JSONObject[urls.size()])) {

			String su = u.get("expanded_url").toString();
			URL url;
			try {
				url = new URL(su);
				writer.print(url.getHost());

			} catch (MalformedURLException e) {
				// TODO Auto-generated catch
				// block
				e.printStackTrace();
			}

		}
		writer.println();
	}

	static void writeEntity(JSONObject object, PrintWriter writer) {

		JSONArray hashtags = (JSONArray) object.get("hashtags");
		if (hashtags != null && hashtags.size() > 0) {
			writeHashTags(hashtags, writer);
		}
		JSONArray urls = (JSONArray) object.get("urls");
		if (urls != null && urls.size() > 0) {
			writeURLs(urls, writer);
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		if (args.length == 0) {
			System.out.println("Usage: java TwitterJsonParser twitterlog");
			System.exit(0);
		}
		File inp = new File(args[0]);
		System.out.println("Reading file :" + inp.getAbsolutePath());
		try {
			FileReader fr = new FileReader(inp);
			BufferedReader reader = new BufferedReader(fr);
			JSONParser parser = new JSONParser(JSONParser.MODE_PERMISSIVE);
			JSONObject jv = null;
			Object o = null;
			while ((o = parser.parse(reader)) != null) {
				if (o instanceof java.lang.String) {
					System.out.println((String) o);
					break;
				}
				jv = (JSONObject) o;
				Long id = (Long) jv.get("id");
				JSONObject retweeted_status = (JSONObject) jv
						.get("retweeted_status");
				JSONObject user = (JSONObject) jv.get("user");
				JSONObject entities = (JSONObject) jv.get("entities");
				if (retweeted_status != null && user != null) {

					String rtId = (String) retweeted_status.get("id_str");
					String userId = (String) user.get("id_str");
					if (!userIdTweetId.contains(rtId + "|" + userId)) {
						userIdTweetId.add(rtId + "|" + userId);
						File d = new File("/home/vavasthi/tweetscateguser/"
								+ userId);
						if (!d.isDirectory()) {
							d.mkdirs();
						}
						File f = new File(d, rtId);
						if (!f.canWrite()) {
							f.createNewFile();
							f.setWritable(true);
						}
						PrintWriter writer = new PrintWriter(f, "UTF-8");

						writeText(retweeted_status, writer);
						if (entities != null) {

							writeEntity(entities, writer);
						}
						writer.close();
					}
				}
			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
